package com.skywolf.chem.pageconfig.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.common.sysconfig.utils.IpUtils;
import com.skywolf.chem.manager.entity.ColumnVo;
import com.skywolf.chem.match.utils.LocalAddress;
import com.skywolf.chem.match.utils.StringUtils;
import com.skywolf.chem.pageconfig.entity.ColumnInfo;
import com.skywolf.chem.pageconfig.service.IColumnInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 栏目表 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/ct/pageconfig/column-info")
@Api(value = "API-栏目相关", description = "API-栏目相关")
public class ColumnInfoController {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    IColumnInfoService columnInfoService;

    @ApiOperation(value = "获取栏目",notes = "获取栏目，first_page-首页 live_page-直播页")
    @RequestMapping(value = "/getColumns",method = {RequestMethod.POST,RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<ColumnVo> getColumns(HttpServletRequest request) {
        //当前所在城市
        String ip = IpUtils.getIpAddr(request);
        String city = LocalAddress.getAddresses(ip);
        List<ColumnInfo> list = columnInfoService.list(new QueryWrapper<ColumnInfo>()
                .eq("position_name","first_page")
                .eq("city",0)
                .orderByDesc("col_sort"));
        String platformType = request.getHeader("platformType");
        //加入推荐栏目
        if("ctApp".equalsIgnoreCase(platformType)){
            ColumnInfo columnInfo = new ColumnInfo();
            columnInfo.setId(-10000L);
            columnInfo.setColName("推荐");
            columnInfo.setPositionName("first_page");
            list.add(0,columnInfo);
        }
        //加入市州栏目
        if(StringUtils.isNotBlank(city)){
            ColumnInfo columnInfo = columnInfoService.getCity(city);
            if(columnInfo!=null){
                list.add(4,columnInfo);
            }
        }
        if("pc".equalsIgnoreCase(platformType)){
            getPcColumninfo(list);
        }
        ColumnVo columnObj = new ColumnVo();
        columnObj.setFirstPage(list);
        list = columnInfoService.list(new QueryWrapper<ColumnInfo>()
                .eq("position_name","live_page")
                .orderByDesc("col_sort"));
        columnObj.setLivePage(list);
        return RespData.success(columnObj);
    }

    @ApiOperation(value = "获取栏目-新接口",notes = "获取栏目,first_page-首页 live_page-直播页")
    @RequestMapping(value = "/new/getColumns",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<ColumnVo> getColumnsnew(HttpServletRequest request) {
        //当前所在城市
        String ip = IpUtils.getIpAddr(request);
        String city = LocalAddress.getAddresses(ip);
        List<ColumnInfo> list = columnInfoService.list(new QueryWrapper<ColumnInfo>()
                .eq("position_name","first_page")
                .eq("city",0)
                .orderByDesc("col_sort"));
        String platformType = request.getHeader("platformType");
        //加入推荐栏目
        if("ctApp".equalsIgnoreCase(platformType)){
            ColumnInfo columnInfo = new ColumnInfo();
            columnInfo.setId(-10000L);
            columnInfo.setColName("推荐");
            columnInfo.setPositionName("first_page");
            list.add(0,columnInfo);
        }
        if("pc".equalsIgnoreCase(platformType)){
            getPcColumninfo(list);
        }
        ColumnVo columnObj = new ColumnVo();
        //写入首页栏目
        columnObj.setFirstPage(list);
        //写入直播tab栏目
        list = columnInfoService.list(new QueryWrapper<ColumnInfo>()
                .eq("position_name","live_page")
                .orderByDesc("col_sort"));
        columnObj.setLivePage(list);
        //加入市州栏目
        if(StringUtils.isNotBlank(city)){
            List<ColumnInfo> cityCloumn = columnInfoService.getOtherCity(city);
            columnObj.setCityColumn(cityCloumn);
        }
        return RespData.success(columnObj);
    }

    public void getPcColumninfo(List<ColumnInfo> list){
        ColumnInfo info = new ColumnInfo();
        info.setId(-1L);
        info.setColName("直播");
        info.setPositionName("first_page");
        list.add(0,info);

        info = new ColumnInfo();
        info.setId(-2L);
        info.setColName("赛事回放");
        info.setPositionName("first_page");
        list.add(1,info);

        info = new ColumnInfo();
        info.setId(-3L);
        info.setColName("赛事预告");
        info.setPositionName("first_page");
        list.add(2,info);
    }
}
